using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DM.DAO;
using DM.Entity;

namespace DM.Web.Admin.QuanLyThanhVien
{
    public partial class ThemMoiThanhVien : System.Web.UI.Page
    {
        readonly NsdDAO nsdService = new NsdDAO();
        protected void Page_Load(object sender, EventArgs e)
        {
             if (!IsPostBack)
             {
                 LoadForm();
             }
        }

        private void LoadForm()
        {
            if (Request.QueryString["MaNsd"]!=null & Request.QueryString["MaNsd"]!=String.Empty)
            {
                int maNsd = Common.ParseInt32(Request.QueryString["MaNsd"]);
                NSD nsd = nsdService.Get(maNsd);
                txtTenDangNhap.Text = nsd.Tendangnhap;
                txtMatKhau1.Text = nsd.Matkhau;
                txtHoTen.Text = nsd.Hoten;
                txtDiaChi.Text = nsd.Diachi ?? "";
                txtEmail.Text = nsd.Email ?? "";
                txtDiDong.Text = nsd.Didong ?? "";
                txtDienThoai.Text = nsd.Dienthoai ?? "";
                btnSave.Text = "Sửa";
                lbTieuDe.Text = "Sửa thông tin người sử dụng";
            }
            lbTieuDe.Text = "Thêm mới người sử dụng";
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("../TrangChuQuanTri.aspx");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            NSD nsd = new NSD();
            nsd.Manhomnsd = 2;
            nsd.Congty = "DuongMalt";
            nsd.Didong = txtDiDong.Text;
            nsd.Dienthoai = txtDienThoai.Text;
            nsd.Hoten = txtHoTen.Text;
            nsd.Matkhau = txtMatKhau1.Text;
            nsd.Tendangnhap = txtTenDangNhap.Text;
            nsd.Yahoo = "";
            nsd.Msn = "";
            nsd.Diachi = txtDiaChi.Text;
            nsd.Email = txtEmail.Text;
            nsd.Skype = "";

            if (Request.QueryString["MaNsd"] != null & Request.QueryString["MaNsd"] != String.Empty)
            {
                nsd.Ma = Common.ParseInt32(Request.QueryString["MaNsd"]);
                nsdService.Update(nsd);
                Common.Show("Cập nhật thành công");
                Response.Redirect("DanhSachThanhVien.aspx");
            }
            else
            {
                nsdService.Insert(nsd);
                Common.Show("Thêm mới thành công");
                Response.Redirect("DanhSachThanhVien.aspx");
            }
        }
    }
}
